<?php

/**
 * Description of Report_withdrawal_ewalletController
 *
 * @author TEJO MURTI
 */
class RollController extends AdminController {

    public $db;
    public function actionView() {
	$session=Yii::app()->session;

        $sort=new CSort;
        $sort->attributes=array(
            'board_end_datetime'=>array(
                'asc'=>'board_end_datetime',
                'desc'=>'board_end_datetime DESC',
            ),
            'board_id'=>array(
                'asc'=>'board_id',
                'desc'=>'board_id DESC',
            ),
            'board_network_network_mid'=>array(
                'asc'=>'board_network_network_mid',
                'desc'=>'board_network_network_mid DESC',
            ),
        );
        
        
        $data_arr=array(
           'select'=>'*',
           'from'=>'sys_board sb',
           'where'=>'sb.board_is_fly=:flex',
           'params'=>array(':flex'=>'1'),
           'join'=>array(
               'sbn'=>'sys_board_network sbn',
               ),
           'params_join'=>array(
                'sbn'=>'sb.board_leader_board_network_id=sbn.board_network_id',
           ),
       );
       //vo_network::get_member_data($network_id);
//       echo '<pre>';
//       print_r($data_arr);
//       echo '</pre>';
        $data_search=array();
        
        $start= (isset($_GET['start_date'])?ConvertDateHelper::explode_datetime_iso('/', $field_name=$_GET['start_date'], $type="date"):2 );
        $end= (isset($_GET['end_date'])?ConvertDateHelper::explode_datetime_iso('/', $field_name=$_GET['end_date'], $type="date"):1 );
       
        
        $time_start=strtotime($start);
        $time_end=strtotime($end);
      
        
       if(is_numeric($time_start) AND is_numeric($time_end) AND $time_start<= $time_end)
       {
           
           $data_search=array(
            'where'=>'date(sb.board_end_datetime) BETWEEN "'.$start.'"
                       AND "'.$end.'" AND sb.board_is_fly=:flex'
               
               );
       }
       else
       {
          
           Yii::app()->user->setFlash('error','Seharusnya tanggal mulai lebih kecil atau sama dari tanggal akhir.');
       }
       $data_arr_results=array_merge($data_arr,$data_search);
       $results=$this->get_data($data_arr_results);
       
//       echo '<pre>';
       $dataProvider=new CArrayDataProvider($results,
               array(
                   'keyField'=>'board_id',
                   'sort'=>$sort,
//                   array(
//                       'attributes'=>array(
//                           'board_id'
//                       ),
//                   ),
//                   'pagination'=>array(
//                       'pageSize'=>1
//                   ),
               ));
//     echo '<pre>';
//     print_r($dataProvider->rawData);
    //     exit;  
	 //SET TYPE DOWNLOAD FILE 
       if(isset($_POST['button']))
       {

          // $dataProvider=;
           $button=$_POST['button'];
           foreach($button AS $key=>$val)
           {
               $type=$key;
           }

           //$dataProvider=$model_serial_buyer->search_serial_report();
           if($dataProvider->rawData)
           {
               foreach($dataProvider->rawData AS $val)
               {
		  $filter_data[]=array(
                       'Class Id'=>$val['board_id'],
                       'MID'=>$val['board_network_network_mid'],
                       'Member Name'=>vo_network::get_member_name($val["board_network_member_id"]),
                       'Fly Date'=>$val['board_end_datetime'],
                      
                   );

               }
           }
                if(isset($filter_data) AND is_array($filter_data))
                {
                   switch ($type)
                   {
                        case 'xls':
                            ExcelExporterCustom::sendAsXLS($filename='Report Roll', $filter_data, 'Report Roll', $header = true);
                        break;
          default:
                            die('Please make sure your choice. ');
                        break;
                   }
                }




       }

        $this->render('view', 
		array(
		'dataProvider' => $dataProvider,
            ));
    }


    private function get_data($data_arr)
    {
        if(!isset($this->db))
        {
            $this->db = Yii::app()->db->createCommand();
        }
        $this->db->select($data_arr['select']);
	$this->db->from($data_arr['from']);
        if($data_arr['where']!='')
        {
            $this->db->where($data_arr['where'],$data_arr['params']);
        }
	       //  array('not in','network_level_status_log_value','passive'),
               // array('>=','date(network_level_status_log_datetime)',$start_daily)
        if(isset($data_arr['join']) )
        {
            foreach($data_arr['join'] AS $key=>$val)
            {
               
                $this->db->join($val, $data_arr['params_join'][$key]);
            }
        }
        
        if(isset($data_arr['group']) AND trim($data_arr['group'])!='')
        {
            $this->db->group($data_arr['group']);
        }
        if(isset($data_arr['order']) AND trim($data_arr['order'])!='')
        {
            $this->db->order($data_arr['order']);
        }

        return $this->db->queryAll();
    }
}

?>
